<?php

function woc_list(){
	//PASANG JAVASCRIPT
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');	
	$header = array(
		'Tanggal'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Nomor Transaksi'=>array('style'=>'border-bottom:2px solid;width:20%'), 
		'Nama Pasien'=>array('style'=>'border-bottom:2px solid;width:25%'), 
		'Penanggung Jawab'=>array('style'=>'border-bottom:2px solid;width:25%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:15'), 
	);
	if(isset($_GET['key']))
	$query = "SELECT * FROM work_on_call a 
		INNER JOIN karyawan c ON a.karyawan_id = c.karyawan_id
		INNER JOIN biodata d ON d.biodata_id = c.biodata_id
		WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) AND 
		( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' )
		ORDER BY work_on_call_id DESC";
	else
		$query = "SELECT * FROM work_on_call ORDER BY work_on_call_id DESC";

	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while( $ey = my_fetch_array($result) ){
		$editproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=edit&id='.$ey['work_on_call_id'],
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['work_on_call_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$detailproperty = array(
			'href'=>'index.php?com=woc&task=detail&id='.$ey['work_on_call_id'],
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
	
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		if(empty($karyawan['gelar'])){
			$nama_karyawan=ucfirst($karyawan['nama']) ;
		}else{
			$nama_karyawan=trim(ucfirst($karyawan['nama'])).", ".$karyawan['gelar'] ;
		}
		$row[]= array(
			'tanggal'=>date('d - m - Y',strtotime($ey['tanggal'])),
			'nomer'=> $ey['nomor_transaksi'] ,
			'pasien'=> strtoupper($ey['pasien']) ,
			'pj'=>$nama_karyawan ,
			''=>position_text_align($edit_button.' '.$detail_button.' '. $delete_button, 'right')
		);
	}
	 
	$datas = table_rows($row);  
 	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=edit\''),
	//	'Karyawan'=>array('onclick'=>'javascript:location.href=\'index.php?com=spd_karyawan\''),
	); 
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$printmenu = additional_menu_on_list( $menuontop , $search );
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu. table_builder($header , $datas ,  6 , false ,$paging );
}

function woc_form($id=0){
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
			'components/system/js/calendar/calendarDateInput.js'
		)
	);

	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');

	$view = form_header( "woc" , "woc"  );
 	$fields = my_get_data_by_id('work_on_call','work_on_call_id',$id);
	$tanggal = array(
		'name'=>'tanggal',
		'value'=>( isset($_POST['tanggal']) ? $_POST['tanggal'] : ($fields?  date('d-m-Y', strtotime($fields['tanggal'])) : date('d-m-Y') )),
		'id'=>'tanggal' 
	);
	$form_field_tanggal = form_calendar($tanggal); 
	$view .= form_field_display( $form_field_tanggal  , "Tanggal"   );
	$operator = array(
		'name'=>'dr_operator',
		'value'=>( isset($_POST['dr_operator']) ? $_POST['dr_operator'] : $fields['dr_operator']),
		'id'=>'dr_operator' ,
		'size'=>'40' ,
	);
	$form_field_operartor= form_dynamic($operator); 
	$view .= form_field_display( $form_field_operartor  , "Dr. Operator"   );
	$anastesi = array(
		'name'=>'dr_anestesi',
		'value'=>( isset($_POST['dr_anestesi']) ? $_POST['dr_anestesi'] : $fields['dr_anestesi']),
		'id'=>'dr_anestesi' ,
		'size'=>'40' ,
	);
	$form_field_dr_anestesi= form_dynamic($anastesi); 
	$view .= form_field_display( $form_field_dr_anestesi  , "Dr. Anestesi"   );
	
	$tindakan = array(
		'name'=>'tindakan',
		'value'=>( isset($_POST['tindakan']) ? $_POST['tindakan'] : $fields['tindakan']),
		'id'=>'tindakan' ,
		'size'=>'40' ,
	);
	$form_field_tindakan= form_dynamic($tindakan); 
	$view .= form_field_display( $form_field_tindakan , "Tindakan"   );
	
	$opsi = array(
		'Khusus'=>'Khusus',
		'Mayor'=>'Mayor',
		'Sedang'=>'Sedang',
		'Kecil'=>'Kecil',
	);
	$klasifikasi = array(
		'name'=>'klasifikasi',
		'value'=>( isset($_POST['klasifikasi']) ? $_POST['klasifikasi'] : $fields['klasifikasi']),
		'id'=>'klasifikasi' , 
	);
	$form_field_klasifikasi= form_dropdown($klasifikasi,$opsi); 
	$view .= form_field_display( $form_field_klasifikasi , "Klasifikasi"   );
	
	$penanggung_jawab = array(
		'name'=>'pasien',
		'value'=>( isset($_POST['pasien']) ? $_POST['pasien'] : $fields['pasien'] ),
		'id'=>'pasien','typr'=>'text','size'=>'35' 
	);
	$form_field_pasien= form_dynamic($penanggung_jawab );
	$view .= form_field_display( $form_field_pasien  , "Pasien"   );
	
	$nomor_transaksi = array(
		'name'=>'nomor_transaksi',
		'value'=>( isset($_POST['nomor_transaksi']) ? $_POST['nomor_transaksi'] : $fields['nomor_transaksi'] ),
		'id'=>'nomor_transaksi',
		'typr'=>'text',
		'size'=>'35' 
	);
	$form_field_tr= form_dynamic($nomor_transaksi); 
	$view .= form_field_display( $form_field_tr  , "Nomor transaksi"   );
	
	$view .= form_field_display( "&nbsp;"  , "&nbsp;"   );
	$tanggal_surat = array(
		'name'=>'tanggal_surat',
		'value'=>( isset($_POST['tanggal_surat']) ? $_POST['tanggal_surat'] : ($fields?  date('d-m-Y', strtotime($fields['tanggal_surat'])) : date('d-m-Y') )),
		'id'=>'tanggal_surat' 
	);
	$form_field_tanggal_surat = form_calendar($tanggal_surat); 
	$view .= form_field_display( $form_field_tanggal_surat , "Tanggal surat"   );
	$nama_karyawan = '';
	if($fields){
		$karyawan = loaddata_karyawan($fields['karyawan_id']);
		$nama_karyawan = $karyawan['nik'].'/'.$karyawan['nama'] ;
	}
	$karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>( isset($_POST['nama_karyawan']) ? $_POST['nama_karyawan'] :$nama_karyawan ),
		'id'=>'nama_karyawan','typr'=>'text','size'=>'35' 
	);
	$form_field_karyawan = form_dynamic($karyawan );
	$view .= form_field_display( $form_field_karyawan  , "Penanggung jawab"   );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$back = array(
		'value' => ( 'Kembali'),
		'name' => 'simpan', 
		'type'=>'button',
		'onclick'=>'javascript:location.href=(\'index.php?com=woc\')',
		'class'=>'main_button'
	);
	$form_back= form_dynamic($back); 
	
	$view .= form_field_display( $form_submit .' '.$form_back , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function woc_submit($id = 0){
	list($dd,$mm,$yyyy) = explode("-" , $_POST['tanggal']);
	$tanggal = $yyyy.'-'.$mm.'-'.$dd;
	list($dd,$mm,$yyyy) = explode("-" , $_POST['tanggal_surat']);
	$tanggal_surat = $yyyy.'-'.$mm.'-'.$dd;
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	$datas = array(
		'tanggal'=>my_type_data_str($tanggal),
		'tanggal_surat'=>my_type_data_str($tanggal_surat),
		'pasien'=>my_type_data_str($_POST['pasien']),
		'nomor_transaksi'=>my_type_data_str($_POST['nomor_transaksi']),
		'klasifikasi'=>my_type_data_str($_POST['klasifikasi']),
		'tindakan'=>my_type_data_str($_POST['tindakan']),
		'dr_operator'=>my_type_data_str($_POST['dr_operator']),
		'dr_anestesi'=>my_type_data_str($_POST['dr_anestesi']),
		'karyawan_id'=>my_type_data_int($karyawan_id),
	);
	if($id > 0){
		return my_update_record('work_on_call' , 'work_on_call_id', $id ,$datas);
	}
	return my_insert_record('work_on_call',$datas);
}

function remove_woc($id){
	$query = "DELETE FROM work_on_call WHERE work_on_call_id={$id}";
	my_query($query);
	$query = "DELETE FROM work_on_call_detail WHERE work_on_call_id={$id}";
	return my_query($query);
}

function woc_detail_list($woc_id){
	//PASANG JAVASCRIPT
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete_detail&id=\'+id;
			}
			return false;
		}
	');		
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'), 
		'Nama karyawan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'Jam Mulai'=>array('style'=>'border-bottom:2px solid;width:15%'), 
		'Jam Selesai'=>array('style'=>'border-bottom:2px solid;width:15%'), 
		'Jumlah Jam'=>array('style'=>'border-bottom:2px solid;width:15%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:15'), 
	);
	$query = "SELECT * FROM work_on_call_detail WHERE work_on_call_id=".$woc_id ;
	$result = my_query($query);
	$row = array();
	$i=0;
	while($ey = my_fetch_array($result)){
		$i++;
		
		$editproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=edit_detail&wid='.$woc_id.'&id='.$ey['work_on_call_detail_id'],
			'title'=>'Edit','rel'=>'facebox'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['work_on_call_detail_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		if(empty($karyawan['gelar'])){
			$nama_karyawan=ucfirst($karyawan['nama']) ;
		}else{
			$nama_karyawan=trim(ucfirst($karyawan['nama'])).", ".$karyawan['gelar'] ;
		}
		$row[] = array(
			'#'=>position_text_align($i ,'center'),
			'nik'=>$karyawan['nik'],
			'nama'=>$nama_karyawan,
			'jam_mulai'=>date('H:i',strtotime($ey['jam_mulai'])),
			'jam_selesai'=>date('H:i',strtotime($ey['jam_selesai'])),
			'jumlah'=> gmdate('H:i', (strtotime($ey['jam_selesai']) - strtotime($ey['jam_mulai']) )),
			'op'=>  position_text_align($edit_button.' '.$delete_button  ,'right')
		);
	}
	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript:jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=edit_detail&wid='.$woc_id.'&id=0\' });'),
		'Back'=>array('onclick'=>'javascript:location.href=\'index.php?com=woc\''),
	); 
	$datas = table_rows($row);  
	$printmenu = additional_menu_on_list( $menuontop , '');
	$view = woc_data($woc_id);
	return $view .'<br />'.$printmenu. table_builder($header , $datas ,  6 , false );
}

function calculate_hours($from , $to){ 
	$difference =  $to - $from ;
	$hours = $difference / 3600; // 3600 seconds in an hour
	$minutes = ($hours - floor($hours)) * 60; 
	$final_hours = sprintf('%02d',round($hours,0));
	$final_minutes = sprintf('%02d',round($minutes));
	return $final_hours.':'.$final_minutes;
}

function woc_data($woc_id){
	$view = form_header( "work_on_call" , "work_on_call"  );
 	$fields = my_get_data_by_id('work_on_call','work_on_call_id',$woc_id); 
	$view .= form_field_display( date('d - m -Y',strtotime($fields['tanggal']) )  , "Tanggal"  );
	$view .= form_field_display( $fields['dr_operator']  , "Dr. Operator"  );
	$view .= form_field_display( $fields['dr_anestesi']  , "Dr. Anestesi"  );
	$view .= form_field_display( $fields['tindakan']  , "Tindakan"  );
	 
	$view .= form_footer( );
	return $view;
}

function delete_woc_detail_woc($id){
	$query = "DELETE FROM work_on_call_detail WHERE work_on_call_detail_id={$id}";
	return my_query($query);
}

function woc_detail_form($wid,$id=0){
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
		)
	);

	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);

 	$fields = my_get_data_by_id('work_on_call_detail','work_on_call_detail_id',$id); 
	$fieldsx = my_get_data_by_id('work_on_call','work_on_call_id',$wid); 
	 
	list($yyyy,$mm,$dd) = explode("-",$fieldsx['tanggal']);
	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
		
		function getShift( ){ 
			var  da = \''.$fieldsx['tanggal'].'\';
			var name = $(\'#nama_karyawan\').val();
			$.get("get_karyawan_schedule.php", { nama: name , date:da },
			   function(data){
					var mySplitResult = data.split("|");  
					if(mySplitResult[3] == 1){
						alert(\'Golongan Jabatan Karyawan Tidak Dapat Lembur\');
					}
					$(\'#jadwal_kerja\').replaceWith(\'<span id="jadwal_kerja">\'+ mySplitResult[0] +\'</span>\');
					$(\'#realisasi_kerja\').replaceWith(\'<span id="realisasi_kerja">\'+ mySplitResult[1] +\'</span>\');
					$(\'#administrasi\').replaceWith(\'<span id="administrasi">\'+ mySplitResult[2] +\'</span>\');
					return false;
				 
			   });
		}
	');

	$view = form_header( "woc" , "woc"  );
	
	$view .= form_field_display( '
	<span id="tanggal_Day_ID">'.$dd.'</span> - 
	<span id="tanggal_Month_ID">'.$mm.'</span>  -
	<span id="tanggal_Year_ID">'.$yyyy.'</span>  
	', "Tanggal"  );
	$view .= form_field_display( $fieldsx['dr_operator']  , "Dr. Operator"  );
	$view .= form_field_display( $fieldsx['dr_anestesi']  , "Dr. Anestesi"  );
	$view .= form_field_display( $fieldsx['tindakan']  , "Tindakan"  );
	
	$tombol_check = array(
		'value' => ( 'Chk'),
		'name' => 'simpan', 
		'type'=>'button',
		'onclick'=>'javascript:getShift()',
		'class'=>'simple_button'
	);
	$form_tombol_check= form_dynamic($tombol_check); 
	
	$nama_karyawan='';
	if($fields){
		$karyawan = loaddata_karyawan($fields['karyawan_id']);
		$nama_karyawan = $karyawan['nik'].'/'.$karyawan['nama'] ;
	}
	$karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>( isset($_POST['nama_karyawan']) ? $_POST['nama_karyawan'] :$nama_karyawan ),
		'id'=>'nama_karyawan','type'=>'text','size'=>'35' 
	);
	$form_field_karyawan = form_dynamic($karyawan );
	$view .= form_field_display( $form_field_karyawan .$form_tombol_check , "NIK/Nama Karyawan"   );
	
	$view .= form_field_display(  '<span id="jadwal_kerja"></span>' , "Jadwal kerja"    ); 
	$view .= form_field_display(  '<span id="realisasi_kerja"></span>' , "Realisasi kerja"    ); 
	$view .= form_field_display(  '<span id="administrasi"></span>' , "Administrasi"    ); 
	
	$jam_mulai 	= view_waktu_format($fields['jam_mulai']);
	$jam1 = array(
		'name'=>'jam_mulai',
		'value'=>$jam_mulai ,
		'id'=>'jam_mulai',
	 );
	$view .= form_field_display( form_time($jam1)   , "Jam Mulai"    ); 

	$jam_selesai 	= view_waktu_format($fields['jam_selesai']);
	$jam2 = array(
		'name'=>'jam_selesai',
		'value'=>$jam_selesai ,
		'id'=>'jam_selesai',
	 );
	$view .= form_field_display( form_time($jam2)   , "Jam Selesai"    ) ;
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 

	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function woc_detail_submit($wid,$id=0){
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	$jam_mulai = set_post_time_data('jam_mulai');	
	list($hhc,$mhc)= explode(":" , $jam_mulai );
	$jam_selesai = set_post_time_data('jam_selesai');
	list($hhc,$mhc)= explode(":" , $jam_selesai );	
	$jumlah_jam=(int) date("H",strtotime(time_min($jam_selesai,$jam_mulai)));
	$jumlah_jam=min($jumlah_jam,4);
	// ($jumlah_jam > 4 ) ? 4 :  
	$datas = array(
		'work_on_call_id'=>my_type_data_int($wid),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'jam_mulai'=>my_type_data_str($jam_mulai),
		'jam_selesai'=>my_type_data_str($jam_selesai),
		'jam_selesai'=>my_type_data_str($jam_selesai),
		'jumlah_jam'=>my_type_data_int($jumlah_jam),
	);
	if($id > 0){
		return my_update_record('work_on_call_detail' , 'work_on_call_detail_id', $id ,$datas);
	}
	return my_insert_record('work_on_call_detail',$datas);
}